package com.traffic.between.adaptor;

import java.util.Date;

import org.apache.log4j.Logger;

import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.BetweenProperties;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.KuangChaoServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;


@ChooseProvider(channel = "kuangchao")
public class KuangChaoAdaptor implements IAdaptor {

	private static Logger log = Logger.getLogger(KuangChaoAdaptor.class);

	public void order() {
	
	    OrderBean orderBean = OrderInfoContext.getContext();
	    orderBean.setSendTime(new Date());
		String pkgNo = orderBean.getProductCode();//充值的流量套餐编码或话费金额（元）
		String phoneNo = orderBean.getPhoneNo();
		String transNo = orderBean.getIds();//订购流水号
		String callbackURL = BetweenProperties.CALLBACK_URL+"Kuangchao";
	
	
		String result = KuangChaoServiceImpl.service.order(pkgNo, phoneNo, transNo,callbackURL);
		log.info("between- 狂潮订单KuangChaoService.order() 订购响应response<>:" + result);
		JSONObject json = JSONObject.fromObject(result);
		String resultCode = json.getString("resultCode");

	    if ("0".equals(resultCode)||"24".equals(resultCode)||"10000".equals(resultCode)) {
	    	orderBean.setStatus("11");
	    	orderBean.setMessage(json.getString("resultMsg"));
	    	orderBean.setErrorMsg(json.getString("resultMsg"));
	    	orderBean.setSuppOrderNo(json.getString("transCode"));
		} else {
			orderBean.setStatus("8");
	    	orderBean.setMessage(json.getString("resultMsg"));
	    	orderBean.setErrorMsg(json.getString("resultMsg"));

		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
		
	}

	
	
	
	public void status() {
		
		OrderBean orderBean = OrderInfoContext.getContext();

		String cstmOrderNo = orderBean.getSuppOrderNo();
	    
		if ("".equals(cstmOrderNo)) {
			orderBean.setMessage("该订单业务流水为空");
			orderBean.setErrorMsg("受理中，无订单");
			orderBean.setStatus("");
		}else {
			String result = KuangChaoServiceImpl.service.orderStatus(cstmOrderNo);
			log.info("between- 狂潮订单状态KuangChaoService.orderStatus() 查询响应response<> :" + result);
			
			JSONObject json = JSONObject.fromObject(result);

			String resultCode = json.getString("resultCode");

			if ("1".equals(resultCode)) {
				orderBean.setStatus("7");
				orderBean.setErrorMsg(json.getString("resultMsg"));
				orderBean.setMessage(json.getString("resultMsg"));
			
			} else if ("2".equals(resultCode)||"0".equals(resultCode)||"10".equals(resultCode)||"11".equals(resultCode)||"12".equals(resultCode)||"13".equals(resultCode)||"14".equals(resultCode)||"15".equals(resultCode)||"16".equals(resultCode)||"17".equals(resultCode)||"18".equals(resultCode)||"19".equals(resultCode)||"20".equals(resultCode)||"10000".equals(resultCode)) {
				
				orderBean.setStatus("1");
				orderBean.setErrorMsg(json.getString("resultMsg"));
				orderBean.setMessage(json.getString("resultMsg"));
			} else {
				orderBean.setStatus("8");
				orderBean.setErrorMsg(json.getString("resultMsg"));
				orderBean.setMessage(json.getString("resultMsg"));
			}
		}
		
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

}
